package com.audiocn.engine;

import java.util.Stack;

import android.content.Context;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ViewAnimator;

import com.audiocn.dc.BaseDC;
import com.audiocn.dc.InitDC;
import com.audiocn.manager.BaseManager;
import com.audiocn.poster.Application;

public class DCEngine implements AnimationListener {
	public enum AnimationStyle {
		fromBottom, fromLeft, fromTop, fromRight, fromCenter, fromAlpha,
		none
	}

	private ViewAnimator viewSwiter;
	private TranslateAnimation animNone;
	private TranslateAnimation animSlideInLeft;
	private TranslateAnimation animSlideOutLeft;
	private TranslateAnimation animSlideInRight;
	private TranslateAnimation animSlideOutRight;
	private ScaleAnimation scaleAnimation;
	private AlphaAnimation alphaAnimation;

	public Context context;
	public boolean isClickEnabled = true;
	private Stack<BaseDC> stack = new Stack<BaseDC>();
	int ScreenHeight, ScreenWidth;

	BaseManager currentManager = null;

	BaseDC currentDC = null;

	public DCEngine(Context c) {
		context = c;
		WindowManager wm = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		ScreenHeight = wm.getDefaultDisplay().getHeight();
		ScreenWidth = wm.getDefaultDisplay().getWidth();
		viewSwiter = new ViewAnimator(context);
		viewSwiter.setDrawingCacheEnabled(false);
		animNone = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
		animNone.setDuration(1);
		animSlideInLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
				1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f);
		animSlideInLeft.setDuration(500);
		animSlideOutLeft = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, -1.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f);
		animSlideOutLeft.setDuration(500);
		animSlideInRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
				-1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f);
		animSlideInRight.setDuration(500);
		animSlideOutRight = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f);
		animSlideOutRight.setDuration(500);
		alphaAnimation = new AlphaAnimation((float) 0.5, 1);
		alphaAnimation.setDuration(500);
		makeScaleAnimation();
		scaleAnimation.setDuration(1000);

		scaleAnimation.setAnimationListener(this);
		alphaAnimation.setAnimationListener(this);
		animSlideInLeft.setAnimationListener(this);
		animSlideOutLeft.setAnimationListener(this);
		animSlideInRight.setAnimationListener(this);
		animSlideOutRight.setAnimationListener(this);
	}

	/**
	 * 显示等待初始化的界面 初始化刚开始进入软件界面的动画
	 * 
	 * @author wang wei 
	 */
	public void initDC(InitDC initDC) {
		viewSwiter.removeAllViews();
		viewSwiter.addView(initDC);
		viewSwiter.setAnimation(scaleAnimation);
		viewSwiter.setDisplayedChild(0);
	}

	public void setStyle(AnimationStyle aStyle) {
		switch (aStyle) {
		case fromBottom:

			break;
		case fromTop:

			break;
		case fromLeft:
			viewSwiter.setInAnimation(animSlideInLeft);
			viewSwiter.setOutAnimation(animSlideOutLeft);
			break;
		case fromRight:
			viewSwiter.setInAnimation(animSlideInRight);
			viewSwiter.setOutAnimation(animSlideOutRight);
			break;
		case fromCenter:
			viewSwiter.setAnimation(scaleAnimation);
			break;
		case fromAlpha:
			viewSwiter.setAnimation(alphaAnimation);
			break;
		/*
		 * case fromRotateIn: rotateAnimationOut.setAnimationListener(this);
		 * rotateAnimationIn.setAnimationListener(null);
		 * viewSwiter.setAnimation(rotateAnimationOut); break; case
		 * fromRotateOut: rotateAnimationOut.setAnimationListener(null);
		 * rotateAnimationIn.setAnimationListener(this);
		 * viewSwiter.setAnimation(rotateAnimationIn); break;
		 */
		case none:
			viewSwiter.setInAnimation(null);
			viewSwiter.setOutAnimation(null);
			break;
		default:
			break;
		}
	}

	/**
	 * 不在动画中,用于判断是否相应click
	 * 
	 * @author wang wei
	 */
	public boolean notAnimition() {
		return isClickEnabled;
	}

	/**
	 * 退出此Manager,动态返回到主界面
	 * 
	 * @author wang wei
	 */
	public void quit(AnimationStyle aStyle) {
		if (!isClickEnabled) {
			return;
		}
		viewSwiter.addView(stack.firstElement());
		while (stack.size() > 1) {
			stack.pop();
		}
		setStyle(aStyle);
		viewSwiter.setDisplayedChild(1);
		viewSwiter.removeViewAt(0);
		((BaseDC) viewSwiter.getCurrentView()).onShow();
	}

	/**
	 * 将某dc从历史栈中移除
	 * 
	 * @author wang wei
	 */
	public void removeDCfromStack(BaseDC dc) {
		stack.remove(dc);
	}

	/**
	 * 清除当前显示的前一个DC
	 * 
	 * @author wang wei
	 */
	public void removeLastDC() {
		if (stack.size() > 1) {
			stack.remove(stack.size() - 2);
		}
	}

	/**
	 * 初始化之后,将初始化完毕的MainDC呈现,作为最底层的DC,重复back()方法N次后显示此DC,quit()方法直接返回到此DC,
	 * 要在acitvity中的setContentView()方法前设置
	 * 
	 * @author wang wei
	 */
	public void setMainDC(BaseDC mainDC) {
		viewSwiter.removeAllViews();
		stack.push(mainDC);
		viewSwiter.addView(stack.peek());
		viewSwiter.setAnimation(alphaAnimation);
		viewSwiter.setDisplayedChild(0);
		mainDC.onShow();
	}

	/**
	 * 用于设置到activity中
	 * 
	 * @author wang wei
	 */
	public ViewAnimator getContentView() {
		return viewSwiter;
	}

	/**
	 * 判断正在显示的dc是否是某dc
	 * 
	 * @author wang wei
	 */
	public boolean isShowDC(BaseDC dc) {
		if (viewSwiter.getCurrentView() == dc) {
			return true;
		} else {
			return false;
		}
	}

	public BaseDC getNowDC() {
		if (viewSwiter.getCurrentView() instanceof BaseDC) {
			return (BaseDC) viewSwiter.getCurrentView();
		} else {
			return null;
		}
	}

	public void makeScaleAnimation() {
		if (ScreenWidth == 320 && ScreenHeight == 600) {
			scaleAnimation = new ScaleAnimation(0.1f, 1.3f, 0.1f, 1.1f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
		} else if (ScreenWidth == 320 && ScreenHeight == 800) {
			scaleAnimation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
		} else if (ScreenWidth == 320 && ScreenHeight == 480) {
			scaleAnimation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
		} else if (ScreenWidth == 320 && ScreenHeight == 854) {
			scaleAnimation = new ScaleAnimation(0.1f, 1.2f, 0.1f, 1.1f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
		} else {
			scaleAnimation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f,
					Animation.RELATIVE_TO_SELF, 0.5f,
					Animation.RELATIVE_TO_SELF, 0.5f);
		}
		scaleAnimation.setFillAfter(true);
	}

	private void setClickEnabled(boolean isCleckEnabled) {
		if (isCleckEnabled) {
			viewSwiter.postDelayed(new Runnable() {
				@Override
				public void run() {
					isClickEnabled = true;
				}
			}, 200);
		} else {
			isClickEnabled = false;
		}
	}

	public void showDC(BaseDC dc, int type) {
		if (dc.getParent() != null) {
			if (dc.getParent() instanceof ViewGroup) {
				((ViewGroup) (dc.getParent())).removeView(dc);
			} else {
				return;
			}
		}
		if (type == 0) {
			viewSwiter.removeAllViews();
			setStyle(AnimationStyle.fromAlpha);
			viewSwiter.addView(dc);
			viewSwiter.setDisplayedChild(0);
		} else {
			if (type == 1) {
				setStyle(AnimationStyle.fromLeft);
			} else if (type == 2) {
				setStyle(AnimationStyle.fromRight);
			}
			dc.invalidate();
			viewSwiter.addView(dc);
			viewSwiter.setDisplayedChild(1);
			viewSwiter.removeViewAt(0);
		}
		dc.onShow();
	}

	public void showDC(BaseDC dc) {

	}

	public void setCurrentManager(BaseManager manager) {
		currentManager = manager;
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		setClickEnabled(true);
		if (scaleAnimation == animation) {
			Application.application.enterMainDC();
		}
		if (animation == animSlideOutLeft) {
			currentManager.sendEmptyMessage(BaseManager.MSG_ENTER_SELF_END);
		} else if (animation == animSlideOutRight) {
			currentManager.sendEmptyMessage(BaseManager.MSG_BACK_SELF_END);
		} else if (animation == alphaAnimation) {
			currentManager.sendEmptyMessage(BaseManager.MSG_ENTER_IN_END);
		}
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
	}

	@Override
	public void onAnimationStart(Animation animation) {
		setClickEnabled(false);
	}

	public void changSkin(BaseDC dc) {
		dc.invalidate();
		viewSwiter.removeAllViews();
		viewSwiter.addView(dc);
		viewSwiter.setDisplayedChild(0);
		dc.onShow();
	}
}
